// pages/coupon/myCoupon/myCoupon.js
import {
  postRequest,
  getRequest,
  postParamsRequest
} from '../../../api/request'
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    couponList: [],
    pageNo: 1,
    pageSize: 10,
    loadType: "",
    showCouponList: false,
    couponListInfo:{},
  },

  //打开优惠券弹窗
  openShowCouponList() {
    this.setData({
      showCouponList: true,
    });
  },

  //关闭优惠券弹窗
  hideShowCouponList() {
    this.setData({
      showCouponList: false,
    });
  },

  //显示优惠券详情
  showCouponInfo(e){
    e.currentTarget.dataset.info.coupon.scope = JSON.parse(e.currentTarget.dataset.info.coupon.scope);
    this.setData({
      showCouponList: true,
      couponListInfo: e.currentTarget.dataset.info,
    });
  },

  //跳转页面
  onToCouponPage(e) {
    console.log(e);
    if (e.currentTarget.dataset.type == 1) {
      wx.navigateTo({
        url: '../historyCoupon/historyCoupon',
      })
    } else if (e.currentTarget.dataset.type == 2) {
      wx.navigateTo({
        url: '../getCoupon/getCoupon',
      })
    } else if (e.currentTarget.dataset.type == 3) {
      wx.navigateTo({
        url: '../sendCoupon/sendCoupon',
      })
    }
  },

  //去使用
  toUse() {
    console.log("dd");
    wx.switchTab({
      url: '../../orderFood/orderFood',
    })
  },

  //初始化数据
  initData() {
    this.setData({
      pageNo: 1,
      pageSize: 10,
      loadType: "",
    });
    this.getMemberCoupon(false);
  },

  //获取优惠券,isConcat 是否拼接
  getMemberCoupon(isConcat) {
    let that = this;
    postRequest("/api/vip/member/getMemberCoupon", {
      beid: app.beid,
      memberId: app.myInfo.memberId,
      isUsed: 0,
      pageNo: that.data.pageNo,
      pageSize: that.data.pageSize,
    }).then((res) => {
      console.log(res.data.data);
      if (res.data.data != null) {
        //合并数据
        let list = isConcat ? that.data.couponList.concat(res.data.data.data) : res.data.data.data;
        console.log(list);
        //判断是否还有数据
        let loadType = "";
        if (res.data.data.totalCount == 0) {
          loadType = "loadNull";
        } else {
          if (Math.ceil(res.data.data.totalCount / that.data.pageNo) <= that.data.pageSize) {
            loadType = "loadOver";
          } else {
            loadType = "loadMore";
          }
        }
        that.setData({
          couponList: list,
          loadType: loadType,
          pageNo: that.data.pageNo + 1,
        })
        // 停止下拉刷新效果
        wx.stopPullDownRefresh()
      }
    }).catch((err) => {
      // 停止下拉刷新效果
      wx.stopPullDownRefresh();
      that.setData({
        showStyle: 3,
      })
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.initData();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.initData();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    if (this.data.loadType == "loadMore") {
      this.setData({
        loadType: "",
      });
      this.getMemberCoupon(true);
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})